package com.shendu.task.tasks;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.shendu.task.domain.TaskDoc;
import com.shendu.task.service.TaskService;
import com.shendu.task.util.DateUtil;

/**
 * 未完成的任务备份
 * 
 * @author unsky
 *
 */
@Component
public class TaskNewDayCopy {
	@Autowired
	TaskService taskService;
	@Scheduled(cron="0 0 23 * * *")
	public void backup() {
		TaskDoc task=new TaskDoc();
		task.setChecked(false);
		List<TaskDoc> list = taskService.list(task);
		list.forEach((taskItem)->{
			taskItem.setTaskId(DateUtil.formatDate(new Date(), "yyyyMMddHHmmss")+new Random().nextInt(100));
			Calendar cal=Calendar.getInstance();
			cal.add(Calendar.DAY_OF_MONTH, 1);
			taskItem.setCreateDate(DateUtil.formatDate(cal.getTime(), "yyyy-MM-dd"));
			taskItem.setId(null);
			taskService.addTask(taskItem);
		});
	}
}
